<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script>

    /*验证代码是否符合规则*/
    function validRule(inputId,inputId2){
        var parent = $.trim($("#"+inputId2).val());
        var code = $.trim($("#"+inputId).val());
        if(parent!=null&&parent!=''){
            validDrugUnique(parent,code,inputId);
        }else{
            var regexNumber = /^[A-Z][A-Za-z0-9]$/;
            if(!regexNumber.test(code)){
                toastr.error('父级类别为空时，类别代码应以大写字母开头，数字或者字母结尾，如【AA,A1...】');
                $("#"+inputId).val("");
                return false;
            }else{
                validDrugUnique(parent,code,inputId);
            }
        }
    }
    /*验证药品代码唯一性*/
    function validDrugUnique(parent,code,inputId){
        $.ajax({
            type: "POST",
            url:"${ctx}/drug/drugClassDict/validUnique?parent.id="+parent+"&classCode="+code,
            dataType: "json",
            success: function (data) {
                if (data.code == "success") {
                    return true;
                }else if(data.code == 'error') {
                    $("#"+inputId).val('');
                    toastr.error(data.data);
                    return false;
                }
            },error: function(data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }

</script>
<div class="content-wrap">
        <div class="wrapper" style="bottom: 50px;">
            <ul class="nav nav-tabs">
                <li>
                    <a href="#"  onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugClassDict/index')">药品类别字典表列表</a>
                </li>
                <li class="active">
                    <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugClassDict/form')">
                        <c:choose>
                            <c:when test="${drugClassDict.id==''|| drugClassDict.id==null}">
                                药品类别字典表添加
                            </c:when>
                            <c:otherwise>药品类别字典表修改</c:otherwise>
                        </c:choose>
                    </a>
                </li>
            </ul>
            <section class="panel panel-default">
                <div class="panel-body">
                    <div class="col-lg-12">
                        <form:form id="inputForm" data-parsley-validate="" modelAttribute="drugClassDict" onsubmit="return formSaveLoad('mainCenterDiv','inputForm','${ctx}/drug/drugClassDict/save','${ctx}/drug/drugClassDict/index');" method="post" class="form-horizontal">
                            <form:hidden path="id"/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">父级类别：</label>
                                <div class="col-sm-4">
                                    <sys:treeselect selectedParent="false" id="drugClassDict" name="parent.id" value="${drugClassDict.parent.id}"  update="${not empty drugClassDict.id}" labelName="parent.className" labelValue="${drugClassDict.parent.className}"
                                                    title="药品类别" url="/drug/drugClassDict/treeData" extId="${drugClassDict.id}" cssClass="form-control" />

                                </div>
                            </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">类别代码：</label>
                                    <div class="col-sm-4">
                                        <c:choose>
                                            <c:when test="${drugClassDict.id!=null && drugClassDict.id!=''}">
                                                <form:input path="classCode" value="${fns:subStringLast(drugClassDict.classCode,2 )}" id="classCode" htmlEscape="false" class="form-control" minlength="2" maxlength="2" data-parsley-required="true" readonly="true"/>
                                            </c:when>
                                            <c:otherwise>
                                                <form:input path="classCode" id="classCode" htmlEscape="false" class="form-control" minlength="2" maxlength="2" data-parsley-required="true" onblur="return validRule('classCode','drugClassDictId')"/>
                                            </c:otherwise>
                                        </c:choose>

                                    </div>
                                    <span class="help-inline">父级类别为空时，类别代码应以大写字母开头，数字或者字母结尾，如【AA,A1...】</span>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">类别名称：</label>
                                    <div class="col-sm-4">
                                        <form:input path="className" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注信息：</label>
                                    <div class="col-sm-4">
                                        <form:textarea path="remarks" htmlEscape="false" class="form-control"  maxlength="1000"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存">&nbsp;
                                        <input id="btnCancel" class="btn" type="button" value="返 回" onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugClassDict/index')">
                                </div>
                            </div>
                        </form:form>
                    </div>

                </div>
            </section>
        </div>
    </div>